package com.qf.test;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;

import java.util.UUID;

/**
 * 管道操作
 */
public class Demo4 {

    @Test
    public void test(){
        //1. 创建连接池
        JedisPool pool = new JedisPool("192.168.41.41",6379);
        Jedis jedis = null;

        try {
            //2. 获取连接对象
            jedis = pool.getResource();

            System.out.println(System.currentTimeMillis());
            /**
             * 3. 不用管道，同步10万条数据操作,49秒
            for (int i = 0; i < 100000; i++) {
                jedis.set("user" + i, UUID.randomUUID().toString());
            }
            */

            /**
             * 3. 用管道，同步10万条数据操作 ,1秒不到

            Pipeline pipelined = jedis.pipelined();
            for (int i = 0; i < 100000000; i++) {
                pipelined.set("age" + i,UUID.randomUUID().toString());
            }
            pipelined.sync();
             */
            System.out.println(System.currentTimeMillis());
        } finally {
            //4. 还连接对象
            jedis.close();
        }
    }
}
